<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>For循环优化</title>
</head>

<body>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>
    <button class="btn">Add</button>

    <script>
        let btnArr = document.getElementsByClassName('btn')
        for (let i = 0; i < btnArr.length; i++) {
            console.log(i);
        }
        for (let i = 0; len = btnArr.length, i < len; i++) {
            console.log(i);
        }

        // jsbench.me:方法二的len=btnArr.length赋值，提前获取数组长度，避免i每次遍历去重新获取一次
    </script>

</body>

</html>